// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/init/init.h>
#include <turbo/flags/flag.h>
#include <pollux/common/memory/shared_arbitrator.h>
#include <pollux/exec/memory_reclaimer.h>
#include <pollux/testing/exec/fuzzer_util.h>
#include <pollux/plan/reference_query_runner.h>
#include <pollux/testing/exec/row_number_fuzzer.h>

/// RowNumberFuzzerRunner leverages RowNumberFuzzer and VectorFuzzer to
/// automatically generate and execute the fuzzer. It works as follows:
///
///  1. Plan Generation: Generate two equivalent query plans, one is row-number
///     over ValuesNode and the other is over TableScanNode.
///  2. Executes a variety of logically equivalent query plans and checks the
///     results are the same.
///  3. Rinse and repeat.
///
/// It is used as follows:
///
///  $ ./pollux_row_number_fuzzer --duration_sec 600
///
/// The flags that configure RowNumberFuzzer's behavior are:
///
///  --steps: how many iterations to run.
///  --duration_sec: alternatively, for how many seconds it should run (takes
///          precedence over --steps).
///  --seed: pass a deterministic seed to reproduce the behavior (each iteration
///          will print a seed as part of the logs).
///  --v=1: verbose logging; print a lot more details about the execution.
///  --batch_size: size of input vector batches generated.
///  --num_batches: number if input vector batches to generate.
///  --enable_spill: test plans with spilling enabled.
///  --enable_oom_injection: randomly trigger OOM while executing query plans.
/// e.g:
///
///  $ ./pollux_row_number_fuzzer \
///         --seed 123 \
///         --duration_sec 600 \
///         --v=1

TURBO_FLAG(int64_t,
    seed,
    0,
    "Initial seed for random number generator used to reproduce previous "
    "results (0 means start with random seed).");

TURBO_FLAG(std::string,
    presto_url,
    "",
    "Presto coordinator URI along with port. If set, we use Presto "
    "source of truth. Otherwise, use DuckDB. Example: "
    "--presto_url=http://127.0.0.1:8080");

TURBO_FLAG(uint32_t,
    req_timeout_ms,
    1000,
    "Timeout in milliseconds for HTTP requests made to reference DB, "
    "such as Presto. Example: --req_timeout_ms=2000");

TURBO_FLAG(int64_t,allocator_capacity, 8L << 30, "Allocator capacity in bytes.");

TURBO_FLAG(int64_t,arbitrator_capacity, 6L << 30, "Arbitrator capacity in bytes.");

using namespace kumo::pollux::exec;

int main(int argc, char** argv) {
  // Calls common init functions in the necessary order, initializing
  // singletons, installing proper signal handlers for better debugging
  // experience.
  melon::Init init(&argc, &argv);
  test::setupMemory(FLAGS_allocator_capacity, FLAGS_arbitrator_capacity);
  std::shared_ptr<kumo::pollux::memory::MemoryPool> rootPool{
      kumo::pollux::memory::memoryManager()->addRootPool()};
  auto referenceQueryRunner = test::setupReferenceQueryRunner(
      rootPool.get(),
      FLAGS_presto_url,
      "row_number_fuzzer",
      FLAGS_req_timeout_ms);
  const size_t initialSeed = FLAGS_seed == 0 ? std::time(nullptr) : FLAGS_seed;
  rowNumberFuzzer(initialSeed, std::move(referenceQueryRunner));
}
